package cn.timeapi.Applet.Mall.CatSquare.controller.backend;

import cn.timeapi.Applet.Mall.CatSquare.common.Const;
import cn.timeapi.Applet.Mall.CatSquare.common.ParamsNotNull;
import cn.timeapi.Applet.Mall.CatSquare.common.ResponseCode;
import cn.timeapi.Applet.Mall.CatSquare.common.ServerResponse;
import cn.timeapi.Applet.Mall.CatSquare.entity.ManagementAccount;
import cn.timeapi.Applet.Mall.CatSquare.service.ICategoryService;
import cn.timeapi.Applet.Mall.CatSquare.service.IManagementAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 描述:分类管理模块
 * @author Vincent Vic
 * create 2020-02-16 9:56
 */

@Api("/manage/category/")
@RestController
@RequestMapping("/manage/category/")   //请求地址设置
public class CategoryManageController {

    @Autowired
    private IManagementAccountService iManagementAccountService;

    @Autowired
    private ICategoryService iCategoryService;

    /**
     * 添加分类
     * @param session 会话信息
     * @param categoryName 类名
     * @param parentId 父节点 @RequestParam(value = "parentId",defaultValue = "0") 设置默认值
     * @return
     */
    @ApiOperation("添加分类")
    @GetMapping("add")
    @ResponseBody   //自动序列化Json
    public ServerResponse addCategroy(HttpSession session,@ApiParam("分类名称") @ParamsNotNull String categoryName,@ApiParam("上级id 默认0") @RequestParam(value = "parentId",defaultValue = "0") int parentId){
        //获取当前用户
        ManagementAccount currentuser = (ManagementAccount) session.getAttribute(Const.CURRENT_USER);
        if (currentuser == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }
        if ( !iManagementAccountService.checkAdminRole(currentuser).isSuccess()){//不是管理员
            return ServerResponse.errorMsg("权限不足");
        }

        return iCategoryService.addCategory(categoryName,parentId);
    }

    //修改分类名称
    @ApiOperation("修改分类名称")
    @GetMapping("updata_name")
    @ResponseBody   //自动序列化Json
    public ServerResponse UpdataCategoryName(HttpSession session, @ApiParam("分类id") @ParamsNotNull Integer categoryId, @ApiParam("分类名称") @ParamsNotNull String categoryName){
        //获取当前用户
        ManagementAccount currentuser = (ManagementAccount) session.getAttribute(Const.CURRENT_USER);
        if (currentuser == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }
        if ( !iManagementAccountService.checkAdminRole(currentuser).isSuccess()){//不是管理员
            return ServerResponse.errorMsg("权限不足");
        }

        return iCategoryService.UpdataCategoryName(categoryId,categoryName);
    }

    //得到分类
    @ApiOperation("得到分类")
    @GetMapping("get")
    @ResponseBody   //自动序列化Json
    public ServerResponse getChildrenParallelCategoty(HttpSession session,@ApiParam("分类id 默认0") @RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        //获取当前用户
        ManagementAccount currentuser = (ManagementAccount) session.getAttribute(Const.CURRENT_USER);
        if (currentuser == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }
        if ( !iManagementAccountService.checkAdminRole(currentuser).isSuccess()){//不是管理员
            return ServerResponse.errorMsg("权限不足");
        }

        return iCategoryService.getChildrenParallelCategoty(categoryId);
    }

    //查询当前节点的ID和递归子节点的ID
    @ApiOperation("查询当前节点的ID和递归子节点的ID")
    @GetMapping("get_deep")
    @ResponseBody   //自动序列化Json
    public ServerResponse getCategoryAndDeepChildCategory(HttpSession session,@ApiParam("分类id 默认0") @RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        //获取当前用户
        ManagementAccount currentuser = (ManagementAccount) session.getAttribute(Const.CURRENT_USER);
        if (currentuser == null){
            return ServerResponse.error(ResponseCode.NEED_LOGIN.getCode(),"未登录，需要登录");
        }
        if ( !iManagementAccountService.checkAdminRole(currentuser).isSuccess()){//不是管理员
            return ServerResponse.errorMsg("权限不足");
        }
        return iCategoryService.getChildrenParallelCategoty(categoryId);
    }


}

